package org.ehe.finance.currency.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.satoken.utils.LoginHelper;
import org.ehe.finance.currency.domain.ErpFinanceCurrency;
import org.ehe.finance.currency.domain.bo.ErpFinanceCurrencyBo;
import org.ehe.finance.currency.domain.vo.ErpFinanceCurrencyVo;
import org.ehe.finance.currency.mapper.ErpFinanceCurrencyMapper;
import org.ehe.finance.currency.service.ErpFinanceCurrencyService;
import org.springframework.stereotype.Service;

/**
 * 币种实现类
 * @author :zhangnn
 * @className :ErpFinanceCurrencyServiceImpl
 * @description: TODO
 * @date 2025-08-26 17:00:05
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ErpFinanceCurrencyServiceImpl extends ServiceImpl<ErpFinanceCurrencyMapper, ErpFinanceCurrency>
    implements ErpFinanceCurrencyService {
    /**
     * 分页
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<ErpFinanceCurrencyVo> queryPageList(ErpFinanceCurrencyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpFinanceCurrency> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getCurrencyCode()),ErpFinanceCurrency::getCurrencyCode,bo.getCurrencyCode());
        lqw.like(StringUtils.isNotBlank(bo.getCurrencyName()),ErpFinanceCurrency::getCurrencyName,bo.getCurrencyName());
        lqw.orderByAsc(ErpFinanceCurrency::getCreateTime);

        IPage<ErpFinanceCurrencyVo> page = this.baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 新增
     * @param bo
     * @return
     */
    @Override
    public int insertByBo(ErpFinanceCurrencyBo bo) {
        bo.setTenantId(LoginHelper.getTenantId());
        ErpFinanceCurrency currency = MapstructUtils.convert(bo, ErpFinanceCurrency.class);
        return this.baseMapper.insert(currency);
    }

    /**
     * 修改
     * @param bo
     * @return
     */
    @Override
    public int updateByBo(ErpFinanceCurrencyBo bo) {
        ErpFinanceCurrency currency = MapstructUtils.convert(bo, ErpFinanceCurrency.class);
        return this.baseMapper.updateById(currency);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int deleteById(Long id) {
        return this.baseMapper.deleteById(id);
    }

    /**
     * 校验
     * @param currencyCode
     * @param id
     * @return
     */
    @Override
    public boolean checkCurrencyCodeUnique(String currencyCode, Long id) {
        // 查询相同编码的币种数量
        LambdaQueryWrapper<ErpFinanceCurrency> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ErpFinanceCurrency::getCurrencyCode, currencyCode);
        // 若为修改操作，排除当前ID
        if (id != null) {
            lqw.ne(ErpFinanceCurrency::getId, id);
        }
        // 存在则返回false（不唯一），反之返回true（唯一）
        return this.baseMapper.selectCount(lqw) == 0;
    }
}
